
// 保存按钮
$("#form-member").validate({
    rules: {
    },
    //提交表单数据
    submitHandler:function(form){
        //获取
        var title
        if($("#biaoti").find("option:selected").text()==""){
            title=$("#lunbotu").find("option:selected").text()
        }  else {
            title=$("#lunbotu").find("option:selected").text()+"-"+$("#biaoti").find("option:selected").text()
        }

        var product_category

        if($("#biaoti").find("option:selected").text()==""){
            product_category=$("#lunbotu").val()
        }  else {
            product_category=$("#biaoti").val()
        }
        var floor = $("#floor").val(),
            url = $("#url").val(),
            category_id = getQueryString("category_id")
        if (null==floor){
            layer.msg('楼层数目前限定为6个!', {icon: 6, time: 1500});
            return;
        }
        var time = new Date().format("yyyy-MM-dd hh:mm");
        var quzhi=JSON.parse(web.readCookie("loginData"))
        var formData = new FormData();
        formData.append("started_at",'1000-01-01 00:00:00');
        formData.append("product_category_id",product_category);
        formData.append("category_id",getQueryString("category_id"));
        formData.append("end_in",'1000-01-01 00:00:00');
        formData.append("image_url",$("#url").val());
        formData.append("title",title);
        formData.append("floor",floor);
        formData.append("file",$("#file")[0].files[0]);
        formData.append("created_name",quzhi.user.username);
        formData.append("created_by",quzhi.user.user_ID);
        console.log($("#file")[0].files[0])
        $.ajax({
            url: root.sroreinter.save_resource_conf,
            cache: false,  //禁用缓存
            type: "POST",
            // 告诉jQuery不要去处理发送的数据
            processData : false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.errorCode=="92"){
                    layer.msg('目前楼层只能添加一个', {icon: 6, time: 3000});
                }
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    parent.queryDataFn();
                    setTimeout( function () {
                        cancelLayerNoRefreshFn();
                    },700);
                }
            }
        });
    }
})
function changImg(e){
    var image = new Image();
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 6, time: 2000});
            $("#file").val("")
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {
            image.src =e.target.result ;
            image.onload = function() {
                if(file.size>5*1024*1024){
                    layer.msg('图片不得大于5M!', {icon: 6, time: 2000});
                    $("#file").val("")
                    return;
                }else{
                    if (this.width!=232&&this.height!=340){
                        layer.msg('场景图片大小限制为232*340!', {icon: 6, time: 2000});
                        r=1
                        $("#file").val("")
                        return;
                    } else {
                        $("#myImg").css({width:this.width+"px",height:this.height+"px",cursor:"pointer"});
                        $("#myImg").attr("src",e.target.result);
                    }
                }

            };
        }
    }
}
$('#pic').click(function (event) {
    event.preventDefault()
    $('#file').click()
})
//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        //console.log(data)
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);

}

/*
第一个接口的下拉框的展示
 */
$(function () {
    $.ajax({
        type: "get",
        //志哥接口
        url: root.sroreinter.product_lable_listLable,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            //给志哥传的数据
            // category_level:1
            // lableLevel:"1",
        },
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#lunbotu").html(interText(dataInter));
                console.log(dataInter)
                var member='<option value="">请选择一级场景</option>';
                $('#lunbotu').append(member)
                $.each(dataInter,function(inx,data2){
                    if(data2.name==""){
                        var member='<option value="'+data2.lable_id+'" >'+data2.lable_name+'</option>';
                        $('#lunbotu').append(member)
                    }else {
                        var member='<option value="'+data2.lable_id+'" >'+data2.lable_name+'</option>';
                        $('#lunbotu').append(member)
                    }
                })

            }
        }
    })
})
function discount(lable_id) {
    console.log(lable_id)
    $.ajax({
        type: "post",
        //志哥接口
        url: root.goodsInter.goods_listSubLable,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            //给志哥传的数据
            lable_id:lable_id,
        },
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#biaoti").html(interText(dataInter));
                console.log(dataInter)
                $.each(dataInter,function(inx,data2){
                    if(data2.name==""){
                        var member='<option value="'+data2.lable_id+'" >'+data2.lable_name+'</option>';
                        $('#biaoti').append(member)
                    }else {
                        var member='<option value="'+data2.lable_id+'" >'+data2.lable_name+'</option>';
                        $('#biaoti').append(member)
                    }
                })

            }
        }
    })
}
Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}
/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.select_floor,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {

        },
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#floor").html(interText(dataInter));
                console.log(dataInter)
                $.each(dataInter,function(inx,data2){
                    console.log(data2)
                    if(data2.name==""){
                        var member='<option selected value="'+data2.name+'" >'+"F"+data2.name+"楼层"+'</option>';
                        $('#floor').append(member)
                    }else {
                        var member='<option selected value="'+data2.name+'" >'+"F"+data2.name+"楼层"+'</option>';
                        $('#floor').append(member)
                    }
                })

            }
        }
    })
})